package car.aistar.service.impl;

import car.aistar.dao.LaoxuPerson;
import car.aistar.dto.CarOverview;
import car.aistar.dto.LaoxuPersonDto;
import car.aistar.service.LaoxuPersonService;
import car.aistar.util.XrbUtil;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.alibaba.druid.sql.PagerUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName LaoxuPersonServiceImpl
 * @Description TODO 个人中心数据展示sql实现类
 * @Author laoxu
 * @Date 2020/10/8 15:42
 * Version 1.0
 **/

@Service("LaoxuPersonService")
public class LaoxuPersonServiceImpl implements LaoxuPersonService {

    @Autowired
    private LaoxuPerson laoxuPerson;

    @Override
    public List<LaoxuPersonDto> queryByHistory(Long uid) {
        return this.laoxuPerson.queryByHistory(uid);
    }

    @Override
    public PageInfo<CarOverview> queryPageHistory(Long uid, Integer pageNow, Integer pageSize) {
        //开启分页
        PageHelper.startPage(pageNow,pageSize);
        //从数据库中查询出
        List<LaoxuPersonDto> laoxuPersonDtos = queryByHistory(uid);

        //封装list到PageInfo对象中自动分页
        PageInfo<LaoxuPersonDto> laoxuPersonDtoPageInfo = new PageInfo<>(laoxuPersonDtos);

        //转换为CarOverview类型的PageInfo对象
        PageInfo<CarOverview> carOverviewPageInfo = XrbUtil.PageInfo2PageInfoVo(laoxuPersonDtoPageInfo);
        //创建需要分页的carOverviewList
        List<CarOverview> carOverviewList = new ArrayList<>();

        //遍历laoxuPersonDtos中LaoxuPersonDto对象给carOverviewList初始化
        for (LaoxuPersonDto d : laoxuPersonDtos) {
            CarOverview carOverview = new CarOverview();
            //将两个对象相同属性的值赋值到carOverview中
            BeanUtils.copyProperties(d,carOverview);

            int y = DateUtil.year(d.getcRegistration());
            carOverview.setcRegistration(y);
            carOverviewList.add(carOverview);
        }

        for (CarOverview c : carOverviewList) {
            carOverviewPageInfo.getList().add(c);
        }
        carOverviewPageInfo.setSize(laoxuPersonDtoPageInfo.getSize());
        return carOverviewPageInfo;
    }


}
